<template>
  <div class="anomaly-container">
    <Navbar title="异常指标" />

    <div ref="refList" class="content-list">
      <div class="anomaly-zb">
        <div class="anomaly-zb-lt">
          <div class="img"></div>
          <div class="font">监测指标</div>
          <div class="num">{{ name.data.total || 0 }}</div>
        </div>
        <div class="anomaly-zb-rg">
          <div class="img abnormaImg"></div>
          <div class="font">异常指标</div>
          <div class="num abnormalNum">{{ name.data.anomalyIndex || 0 }}</div>
        </div>
      </div>
      <div class="echartClass" v-if="name.data.anomalyTotal?.length > 0">
        <div class="road"></div>
        <div class="echarts-box"
          v-for="item in !isExpand ? copyAnomalyTotal : name.data.anomalyTotal"
          :key="item"
        >
          <div class="echarts-title">{{ item.name }}</div>
          <div class="echarts-lint">
            <div class="echarts-left">
              <div class="echarts-pad" :style="{ width: item.rate }"></div>
            </div>
            <div class="echarts-num">
              <div class="value">{{ item.value }}</div>
              <div class="numlin"></div>
              <div>{{ item.rate }}</div>
            </div>
          </div>
        </div>
      </div>
      <div
        class="more"
        v-if="name.data.anomalyTotal !== [] && !apiCode && name.data.anomalyTotal.length >= 5"
      >
        <div class="more-name" @click="moreClick">{{ !isExpand ? '更多' : '收起' }}</div>
        <SvgIcon style="font-size: 14px" :icon-class="'more'" :class="{ 'retract-up': isExpand }">
        </SvgIcon>
      </div>
      <QuotaList :data="name.data.anomalyInfo"></QuotaList>
    </div>
    
    <div
      class="videoMeet"
      ref="videoMeetRef"
      :draggable="true"
      @click="meetShow ? handleViewAll() : videoMeetShow()"
      @touchstart="mousedown"
      @touchmove="meetMove"
      @touchend="meetEnd"
    ></div>
  </div>
</template>

<script lang="ts" setup>
import SvgIcon from '@/components/SvgIcon'
import QuotaList from './components/QuotaList.vue'
import anomalyServe from '@/api/anomalyIndex'
import { ref, reactive, onMounted, inject } from 'vue'
import { getVideoUrl } from '@/utils/index'
import { useRouter, useRoute } from 'vue-router'

defineOptions({
  name: 'AnomalyIndex'
})

let apiCode = ref(true)
let isExpand = ref(false)
let copyAnomalyTotal = reactive([])
const name = reactive({
  data: {}
})

const moreClick = () => {
  isExpand.value = !isExpand.value
}

const getAnomalyIndexCount = async () => {
  apiCode.value = true
  const res = await anomalyServe.getAnomalyIndexCount()
  if (!res.code == 0) return
  name.data = res.data
  //由于后端逻辑问题，需要前端去重
  name.data.anomalyTotal = [...new Set(res.data.anomalyTotal.map(t => JSON.stringify(t)))].map(s =>
    JSON.parse(s)
  )
  if (name.data.anomalyTotal.length >= 5) {
    copyAnomalyTotal = name.data.anomalyTotal.slice(0, 5)
  } else {
    copyAnomalyTotal = name.data.anomalyTotal
  }
  apiCode.value = false
}
getAnomalyIndexCount()

const videoMeetRef = ref(null)
const meetShow = ref(false)
const isMeetMove = ref(false)
const videoMeetShow = (e: any) => {
  meetShow.value = !meetShow.value
  videoMeetRef._value.style.right = meetShow.value ? '15px' : '-40px'
  setTimeout(() => {
    meetShow.value = false
    videoMeetRef._value.style.right = '-40px'
  }, 1000)
}

const meetMove = (ev: any) => {
  isMeetMove.value = true
  videoMeetRef._value.style.right = '15px'
  videoMeetRef._value.style.left =
    ev.touches[0].pageX - videoMeetRef._value.clientWidth / 2 + 'px'
  videoMeetRef._value.style.top =
    ev.touches[0].pageY - videoMeetRef._value.clientHeight / 2 + 'px'
}
const meetEnd = (e: any) => {
  if (isMeetMove.value) {
    console.log('end')
    videoMeetRef._value.style.left = ''
    videoMeetRef._value.style.right = '15px'
    videoMeetShow()
  }
  isMeetMove.value = false
}

const router = useRouter()
const handleViewAll = (type: string) => {
  const url = getVideoUrl();
  window.open(url, '_blank')
}
</script>

<style lang="less" scoped>
.anomaly-container {
  height: 100vh;
  box-sizing: border-box;
  overflow: auto;
  display: flex;
  flex-direction: column;
  .content-list {
    flex-grow: 1;
    overflow-y: scroll;
    padding: 12px 15px 20px;
    .anomaly-zb {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 10px;
      .anomaly-zb-lt,
      .anomaly-zb-rg {
        display: flex;
        align-items: center;
        width: 169px;
        height: 52px;
        border-radius: 4px;
        padding: 0 12px;
        box-sizing: border-box;
        background-color: rgba(0, 102, 255, 0.05);
        .font {
          margin: 0 1.8667vw;
          font-size: 14px;
          color: #212121;
        }
        .num {
          font-size: 18px;
          font-family: 'DINPro';
          color: #1f2133;
        }
        .abnormalNum {
          color: #e83a48;
        }
        .img {
          width: 34px;
          height: 34px;
          background: url(@/assets/keyIndicator/monitor.png) no-repeat;
          background-size: 34px 34px;
        }
      }
      .anomaly-zb-rg {
        background: linear-gradient(to left, rgba(242, 36, 56, 0.08), rgba(255, 77, 94, 0.08));
        .abnormaImg {
          background: url(@/assets/keyIndicator/abnormal.png) no-repeat;
          background-size: 34px 34px;
        }
      }
    }
    .echartClass {
      width: 100%;
      background: #ffffff;
      border-radius: 8px;
      box-sizing: border-box;
      position: relative;
      margin-top: 6px;
      padding: 10px 15px 5px;
      .road {
        width: 100%;
        background: url(@/assets/keyIndicator/red-road.png) no-repeat;
        background-size: 100% 7px;
        height: 7px;
        position: absolute;
        top: -4px;
        right: 0;
      }
      .echarts-box {
        display: flex;
        flex-direction: row;
        align-items: center;
        height: 30px;
        .echarts-title {
          min-width: 70px;
        }
        .echarts-lint {
          display: flex;
          justify-content: space-between;

          .echarts-left {
            width: 38vw;
            margin-left: 2vw;
            height: 2vw;
            margin-right: 2vw;
            background-color: rgba(232, 58, 72, 0.08);
            border-radius: 0 12px 12px 0;
            display: flex;
            align-items: center;
            .echarts-pad {
              height: 60%;
              border-radius: 0 12px 12px 0;
              background: linear-gradient(90deg, #ff8095 100%, #e84661 100%);
            }
          }

          .echarts-num {
            display: flex;
            align-items: center;
            font-weight: bold;
            margin-left: 2vw;
            .value {
              color: #e83a48;
            }
            .numlin {
              width: 1px;
              transform: scaleX(0.5);
              height: 2vw;
              background: #000;
              margin: 0 5px;
            }
          }
        }
      }
    }
    .more {
      height: 33px;
      border-top: 1px solid #e2e2e2;
      background: #ffffff;
      border-radius: 0 0 8px 8px;
      display: flex;
      align-items: center;
      justify-content: center;
      .more-name {
        font-weight: 500;
        color: #888888;
        font-size: 13px;
      }

      .retract-up {
        transform: rotate(180deg);
      }

      .more-img {
        width: 14px;
        height: 14px;
        background: url(@/assets/anomalyIndex/bottom.png) no-repeat;
        background-size: 100% 100%;
        margin-left: 5px;
      }
    }
    :deep(.index-item-one) {
      background: linear-gradient(to left, rgba(242, 36, 56, 0.08), rgba(255, 77, 94, 0.08));
    }
  }
  .videoMeet{
    position: fixed;
    width: 80px;
    height: 80px;
    transition: right 0.2s ease-in;
    background: url('/src/assets/keyIndicator/icon-videomeet.png') no-repeat;
    background-size: 100% 100%;
    right: -40px;
    bottom: 15px;
    z-index: 99;
  }
}
</style>
